﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace Module.QuanLyDuoc.HeThongNhapXuat
{
    public class PhieuXuatKhoSangKho : DBInteractionBase
    {
        // Phiếu xuất kho sang kho
        public string MaPhieu { get; set; }
        public string MaKho { get; set; }
        public string MaPhieuYeuCau { get; set; }
        public DateTime NgayXuat { get; set; }
        public string NguoiXuat { get; set; }
        public string MaNguoiXetDuyet { get; set; }
        public int IsXetDuyet { get; set; }
        public string NguoiTao { get; set; }
        // Phiếu xuất kho sang kho chi tiết
        public string ID { get; set; }
        public string MaVatTu { get; set; }
        public string MaDonVi { get; set; }
        public int SoLuongYeuCau { get; set; }
        public int SoLuong { get; set; }
        public double DonGia { get; set; }
        public double ThanhTien { get; set; }
        public string LoSanXuat { get; set; }
        public DateTime HanSuDung { get; set; }
        public string MaPhieuNhap { get; set; }
        public string GhiChu { get; set; }

        public DataTable PhieuXuatKhoSangKho_SelectAll(DateTime tungay, DateTime denngay, string MaKho, int pageindex, int pagesize, out int recordcount)
        {
            var dt = new DataSet();
            try
            {
                using (var dbcmd = new SqlCommand("tbDuoc_PhieuXuatKhoSangKho_SelectAll", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@TuNgay", tungay));
                    dbcmd.Parameters.Add(new SqlParameter("@DenNgay", denngay));
                    dbcmd.Parameters.Add(new SqlParameter("@MaKho", MaKho));
                    dbcmd.Parameters.Add(new SqlParameter("@PageIndex", pageindex));
                    dbcmd.Parameters.Add(new SqlParameter("@PageSize", pagesize));
                    dbcmd.Parameters.Add(new SqlParameter("@RecordCount", SqlDbType.Int, 50)
                    {
                        Direction = ParameterDirection.Output
                    });
                    var dtAdapter = new SqlDataAdapter(dbcmd);
                    dtAdapter.Fill(dt);
                    recordcount = Convert.ToInt32(dbcmd.Parameters["@RecordCount"].Value);
                    return dt.Tables[0];
                }
            }
            catch (Exception ex)
            {
                throw new Exception("tbDuoc_PhieuXuatKhoSangKho_SelectAll: " + ex.Message);
            }
        }

        public bool PhieuXuatKhoSangKho_Insert(PhieuXuatKhoSangKho obj)
        {
            try
            {
                using (var dbcmd = new SqlCommand("tbDuoc_PhieuXuatKhoSangKho_Insert", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@MaPhieu_Output", SqlDbType.VarChar)
                    {
                        Direction = ParameterDirection.Output,
                        Size = 50
                    });
                    dbcmd.Parameters.Add(new SqlParameter("@MaPhieuYeuCau", obj.MaPhieuYeuCau));
                    dbcmd.Parameters.Add(new SqlParameter("@NgayXuat", obj.NgayXuat));
                    dbcmd.Parameters.Add(new SqlParameter("@NguoiXuat", obj.NguoiXuat));
                    dbcmd.Parameters.Add(new SqlParameter("@NguoiTao", obj.NguoiTao));
                    dbcmd.ExecuteNonQuery();
                    obj.MaPhieu = dbcmd.Parameters["@MaPhieu_Output"].Value.ToString();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool PhieuXuatKhoSangKho_Update(PhieuXuatKhoSangKho obj)
        {
            try
            {
                using (var dbcmd = new SqlCommand("tbDuoc_PhieuXuatKhoSangKho_Update", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@MaPhieu", obj.MaPhieu));
                    dbcmd.Parameters.Add(new SqlParameter("@MaPhieuYeuCau", obj.MaPhieuYeuCau));
                    dbcmd.Parameters.Add(new SqlParameter("@NgayXuat", obj.NgayXuat));
                    dbcmd.Parameters.Add(new SqlParameter("@NguoiXuat", obj.NguoiXuat));
                    dbcmd.Parameters.Add(new SqlParameter("@NguoiTao", obj.NguoiTao));
                    dbcmd.ExecuteNonQuery();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool PhieuXuatKhoSangKho_Delete(string maphieu)
        {
            try
            {
                using (var dbcmd = new SqlCommand("tbDuoc_PhieuXuatKhoSangKho_Delete", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@MaPhieu", maphieu));
                    dbcmd.ExecuteNonQuery();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool PhieuXuatKhoSangKho_UpdateDuyetPhieu(string maphieu, string manguoixetduyet, int isxetduyet, string ghichu)
        {
            try
            {
                using (var dbcmd = new SqlCommand("tbDuoc_PhieuXuatKhoSangKho_UpdateDuyetPhieu", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@MaPhieu", maphieu));
                    dbcmd.Parameters.Add(new SqlParameter("@MaNguoiXetDuyet", manguoixetduyet));
                    dbcmd.Parameters.Add(new SqlParameter("@IsXetDuyet", isxetduyet));
                    dbcmd.Parameters.Add(new SqlParameter("@GhiChu", ghichu));
                    dbcmd.ExecuteNonQuery();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public DataTable PhieuXuatKhoSangKho_SelectById(string maphieu)
        {
            var dt = new DataSet();
            try
            {
                using (var dbcmd = new SqlCommand("tbDuoc_PhieuXuatKhoSangKho_SelectById", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@MaPhieu", maphieu));
                    var dtAdapter = new SqlDataAdapter(dbcmd);
                    dtAdapter.Fill(dt);
                    return dt.Tables[0];
                }
            }
            catch (Exception ex)
            {
                throw new Exception("PhieuXuatKhoSangKho_SelectById: " + ex.Message);
            }
        }

        public bool PhieuXuatKhoSangKho_ChiTiet_Insert(PhieuXuatKhoSangKho obj)
        {
            try
            {
                using (var dbcmd = new SqlCommand("tbDuoc_PhieuXuatKhoSangKho_ChiTiet_Insert", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@MaPhieu", obj.MaPhieu));
                    dbcmd.Parameters.Add(new SqlParameter("@MaKho", obj.MaKho));
                    dbcmd.Parameters.Add(new SqlParameter("@MaVatTu", obj.MaVatTu));
                    dbcmd.Parameters.Add(new SqlParameter("@SoLuong", obj.SoLuongYeuCau));
                    dbcmd.Parameters.Add(new SqlParameter("@GhiChu", obj.GhiChu));
                    dbcmd.Parameters.Add(new SqlParameter("@NguoiTao", obj.NguoiTao));
                    dbcmd.ExecuteNonQuery();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool PhieuXuatKhoSangKho_ChiTiet_Delete(string maphieu, string mavattu)
        {
            try
            {
                using (var dbcmd = new SqlCommand("tbDuoc_PhieuXuatKhoSangKho_ChiTiet_Delete", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@MaPhieuXuatKhoSangKho", maphieu));
                    dbcmd.Parameters.Add(new SqlParameter("@MaVatTu", mavattu));
                    dbcmd.ExecuteNonQuery();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public DataTable PhieuXuatKhoSangKho_ChiTiet_SelectByMaPhieu(string maphieu)
        {
            var dt = new DataSet();
            try
            {
                using (var dbcmd = new SqlCommand("tbDuoc_PhieuXuatKhoSangKho_ChiTiet_SelectByMaPhieu", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@MaPhieuXuatKhoSangKho", maphieu));
                    var dtAdapter = new SqlDataAdapter(dbcmd);
                    dtAdapter.Fill(dt);
                    return dt.Tables[0];
                }
            }
            catch (Exception ex)
            {
                throw new Exception("PhieuXuatKhoSangKho_ChiTiet_SelectByMaPhieu: " + ex.Message);
            }
        }

        public DataTable PhieuXuatKhoSangKho_ChiTiet_SelectByMaPhieu_ChiTiet(string maphieu)
        {
            var dt = new DataSet();
            try
            {
                using (var dbcmd = new SqlCommand("tbDuoc_PhieuXuatKhoSangKho_ChiTiet_SelectByMaPhieu_ChiTiet", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@MaPhieuXuatKhoSangKho", maphieu));
                    var dtAdapter = new SqlDataAdapter(dbcmd);
                    dtAdapter.Fill(dt);
                    return dt.Tables[0];
                }
            }
            catch (Exception ex)
            {
                throw new Exception("tbDuoc_PhieuXuatKhoSangKho_ChiTiet_SelectByMaPhieu: " + ex.Message);
            }
        }

        public DataTable PhieuXuatKhoSangKho_ChiTiet_SelectByMaPhieuMaVatTu(string maphieu, string mavattu)
        {
            var dt = new DataSet();
            try
            {
                using (var dbcmd = new SqlCommand("tbDuoc_PhieuXuatKhoSangKho_ChiTiet_SelectByMaPhieuMaVatTu", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@MaPhieuXuatKhoSangKho", maphieu));
                    dbcmd.Parameters.Add(new SqlParameter("@MaVatTu", mavattu));
                    var dtAdapter = new SqlDataAdapter(dbcmd);
                    dtAdapter.Fill(dt);
                    return dt.Tables[0];
                }
            }
            catch (Exception ex)
            {
                throw new Exception("tbDuoc_PhieuXuatKhoSangKho_ChiTiet_SelectByMaPhieuMaVatTu: " + ex.Message);
            }
        }
    }
}